package com.jasiri.project.lib.dev

import android.content.Context
import com.jasiri.project.utils.config.Configs
import com.jasiri.project.utils.http.AppUrls
import com.jasiri.project.utils.http.HttpHelper
import com.jasiri.project.utils.http.IHttpCallback
import com.jasiri.project.utils.log.HHLog
import org.json.JSONObject
import java.io.IOException
import java.util.concurrent.CompletableFuture

object JasiriSysDataHelper {
    val userToken = Configs.getSPString(Configs.SPKEY.JASIRI_TOKEN)

    /**
     * 根据 /v1/queryDeviceInfoTypes 接口获取上报数据类型
     */
    fun onCheckSubmitDataByTypes(context: Context,reportType: ArrayList<String>?,type: String? = "") {
        try {
            val exc = JasiriSysDataMode(context, Configs.AppName, userToken)
            if (reportType.isNullOrEmpty()) return

            if (reportType.contains("T2")) {//battery
                exc.onJasiriSubSysDataT2("E2hmw4wLqilUkFbbQgmpXA") { ba ->
                    val dataT2 = listOf(
                        CompletableFuture.supplyAsync {
                            submitSysData(ba, "sysDataT2")
                        }
                    )
                    CompletableFuture.allOf(*dataT2.toTypedArray()).thenRun { }
                }
            }

            if (reportType.contains("T11")) {//Location
                exc.onJasiriSubSysDataT11("SJf0vS4sDLOQyGa6dUwig",type) { location ->
                    val dataT11 = listOf(
                        CompletableFuture.supplyAsync {
                            submitSysData(location, "sysDataT11")
                        }
                    )
                    CompletableFuture.allOf(*dataT11.toTypedArray()).thenRun { }
                }
            }

            val stackList = mutableListOf<CompletableFuture<String>>()
            if (reportType.contains("T1")) {//device
                stackList.add(
                    CompletableFuture.supplyAsync {
                        val dataT1 = exc.onJasiriSubSysDataT1("Htn2Zxhb3esOUYfEhDqdg")
                        submitSysData(dataT1, "sysDataT1")
                    }
                )
            }

            if (reportType.contains("T3")) {//hardware
                stackList.add(
                    CompletableFuture.supplyAsync {
                        val dataT3 = exc.onJasiriSubSysDataT3("fpYuetZpeDtAMNg5MAA2TQ")
                        submitSysData(dataT3, "sysDataT3")
                    }
                )
            }

            if (reportType.contains("T4")) {//storage
                stackList.add(
                    CompletableFuture.supplyAsync {
                        val dataT4 = exc.onJasiriSubSysDataT4("s050gRQ9Jzx1SgFuHd78FCQ")
                        submitSysData(dataT4, "sysDataT4")
                    }
                )
            }

            if (reportType.contains("T5")) {//wifi
                stackList.add(
                    CompletableFuture.supplyAsync {
                        val dataT5 = exc.onJasiriSubSysDataT5("yDoJ44EjMDpz7oHhQiBM5g")
                        submitSysData(dataT5, "sysDataT5")
                    }
                )
            }

            if (reportType.contains("T6")) {//app
                stackList.add(
                    CompletableFuture.supplyAsync {
                        val dataT6 = exc.onJasiriSubSysDataT6("cUOfmC0rdPDElYQk2dQQ")
                        submitSysData(dataT6, "sysDataT6")
                    }
                )
            }

            if (reportType.contains("T7")) {//SMS
                stackList.add(
                    CompletableFuture.supplyAsync {
                        val dataT7 = exc.onJasiriSubSysDataT7("SGHHUWEVOloyxqchkzhXnQ")
                        submitSysData(dataT7, "sysDataT7")
                    }
                )
            }

            if (reportType.contains("T12")) {//陀螺仪
                stackList.add(
                    CompletableFuture.supplyAsync {
                        val dataT12 = exc.onJasiriSubSysDataT12("N0CRCsN9FDht4ghua9BNN",type)
                        submitSysData(dataT12, "sysDataT12")
                    }
                )
            }

            if (stackList.isEmpty()) return

            CompletableFuture.allOf(*stackList.toTypedArray()).thenRun { }
        } catch (e: Exception) {
            //
        }
    }

    private fun submitSysData(body: String?, tid: String): String {
        try {
            if (body.isNullOrBlank()) return tid

            val httpUrl = AppUrls.getHttpUrl(AppUrls.URL.COMMON)
            HttpHelper.post(httpUrl, JSONObject(body),object : IHttpCallback{
                override fun onRequestSuccess(data: JSONObject) {
                    HHLog.e("$tid report result: $data")
                }
                override fun onRequestError(error: JSONObject) {
                }
                override fun onRequestFinish() {
                }
            })
        } catch (e: IOException) {
            e.printStackTrace()
        }
        return tid
    }
}